using M13.Base;
using QFramework;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace M13.Game.UI
{
    public class UICtrl : MonoBehaviour, IController
    {
        public GameObject debugInfoBoard;
        public GameObject gameMenu;
        private Text _debugInfo;

        public Button[] buttons;

        public bool debugEnabled;
        public bool gameMenuEnabled;

        private float _fps;
        private float _pFPS;

        WaitForSecondsRealtime _wait05 = new WaitForSecondsRealtime(0.5f);
        WaitForSecondsRealtime _wait3 = new WaitForSecondsRealtime(10f);

        public IArchitecture GetArchitecture()
        {
            return GameArchitecture.Interface;
        }
        private void Start()
        {
            this.RegisterEvent<DebugInfoBroadButton>(e => BoardSwitch(e.debuginfoInput, debugInfoBoard));
            this.RegisterEvent<GameMenuButton>(e => BoardSwitch(e.gameMenuInput, gameMenu));

            _debugInfo = debugInfoBoard.GetComponent<Text>();


            for (int i = 0; i < buttons.Length; i++)
            {
                int index = i;
                buttons[i].onClick.AddListener(ButtonRegistered(buttons[index].name));
            }
        }

        private void Update()
        {
            debugEnabled = debugInfoBoard.activeSelf;
            gameMenuEnabled = gameMenu.activeSelf;

            if (!debugEnabled) return;

            _debugInfo.text = ((int)(1 / Time.deltaTime)).ToString();
        }


        private void FixedUpdate()
        {

            ////��ҵ�ǰ״̬
            //_debugInfo.text += "\ncurrentState: " + this.GetModel<IPlayerModal>().playerCurrentState.Value;
            ////���ָ��λ��
            //_debugInfo.text += "\ntargetPosition: " + this.GetModel<ICameraModal>().mouseTarget.Value.ToString();
            ////ָ�����Ʒ����
            //_debugInfo.text += "\nitem: " + this.GetModel<ICameraModal>().itemName.Value;
        }
        void BoardSwitch(float e, GameObject gameObject)
        {
            if (gameObject == null) return;
            if (e == 1f)
                gameObject.SetActive(!gameObject.activeSelf);
        }
        public UnityAction ButtonRegistered(string buttonName)
        {
            //Debug.Log("��ťע��");
            if (buttonName == "ContinueBtn")
                return ContinueGameButton;
            else if (buttonName == "OptionsBtn")
                return OptionsButton;
            else
                return ExitGameButton;
        }

        public void ContinueGameButton()
        {
            Debug.Log("ContinueBtn");
            gameMenuEnabled = false;
            BoardSwitch(1f, gameMenu);
        }
        public void OptionsButton()
        {
            Debug.Log("OptionsBtn");
        }
        public void ExitGameButton()
        {
            Debug.Log("ExitBtn");
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
        }
    }
}